package com.ithengxing.d3_udp2;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

/*
* 多发 多收
*
* */
public class ClientUdp {
    public static void main(String[] args) throws Exception {
        System.out.println("============客户端启动============");
        //创建发送端对象：发送端自带默认的端口号（人）
        DatagramSocket socket = new DatagramSocket(6666);
         Scanner sc = new Scanner(System.in);
         while (true) {
             System.out.println("说你想说的： ");
             String msg = sc.nextLine();

             if("exit".equals(msg)) {
                 System.out.println("已离线！");
                 socket.close();
                 break;
             }
             //创建一个数据包对象封装数据
             byte[] bytes = msg.getBytes();
             DatagramPacket packet = new DatagramPacket(bytes, bytes.length, InetAddress.getLocalHost(), 8888);

             //发送数据
             socket.send(packet);
         }




    }
}
